package com.gjintel.business.enquire.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gjintel.business.enquire.dao.IProjectEnquireDao;
import com.gjintel.business.enquire.model.ProjectEnquire;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

@Repository
@SuppressWarnings("all")
public class ProjectEnquireDaoImpl extends HibernateGenericDAO implements IProjectEnquireDao {

	@Override
	public List<ProjectEnquire> findAll(Page page,Sort sort, ProjectEnquire enquire) {
		if (sort != null && StringUtil.isEmpty(sort.getFields())) {
			sort.setFields("createTime");
			sort.setOrders("desc");
		}
		StringBuilder hql = new StringBuilder("select new ProjectEnquire(enquire.projId,enquire.projName,enquire.senderId,enquire.receiverId"
				+ ",enquire.createTime,enquire.senderName,enquire.receiverName,enquire.resourceId,enquire.resourceName) from ProjectEnquire enquire where 1=1 ");
		List<Object> paramList = new ArrayList<Object>();
		//业务数据
		if(enquire!=null){
			if (!StringUtil.isEmpty(enquire.getProjId())) {
				hql.append(" and enquire.projId = ?");
				paramList.add(enquire.getProjId());
			}
			if (!StringUtil.isEmpty(enquire.getReceiverId())) {
				hql.append(" and enquire.receiverId = ?");
				paramList.add(enquire.getReceiverId());
			}
			
		}
		hql.append(" group by enquire.projId,enquire.senderId");
		return this.findAsPage(hql.toString(), page, sort, "enquire", true, paramList.toArray());
	}

	@Override
	public List<ProjectEnquire> findEnquireList(ProjectEnquire enquire) {
		Sort sort=new Sort();
		if (sort != null && StringUtil.isEmpty(sort.getFields())) {
			sort.setFields("createTime");
			sort.setOrders("asc");
		}
		StringBuilder hql = new StringBuilder("select new ProjectEnquire(enquire.projId,enquire.projName,enquire.senderId,enquire.receiverId"
				+ ",enquire.createTime,enquire.senderName,enquire.receiverName,enquire.resourceId,enquire.resourceName) from ProjectEnquire enquire where 1=1 ");
		List<Object> paramList = new ArrayList<Object>();
		//业务数据
		if(enquire!=null){
			if (!StringUtil.isEmpty(enquire.getProjId())) {
				hql.append(" and enquire.projId = ?");
				paramList.add(enquire.getProjId());
				
				hql.append(" and ((enquire.senderId = ? and enquire.receiverId = ?) or (enquire.senderId = ? and enquire.receiverId = ?))");
				paramList.add(enquire.getSenderId());
				paramList.add(enquire.getReceiverId());
				paramList.add(enquire.getReceiverId());
				paramList.add(enquire.getSenderId());
			}
		}
		return this.findAsPage(hql.toString(), null, sort, "enquire", true, paramList.toArray());
	}
}
